package com.jhy.new2025.BinaryTree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * 给定一棵二叉树的根节点 root ，
 * 请找出该二叉树中每一层的最大值。
 */
public class LargestValues {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        if (root == null) return res;
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            int max = Integer.MIN_VALUE;
            while (size > 0) {
                TreeNode pop = deque.poll();
                int val = pop.val;
                max = Math.max(val, max);
                if(pop.right != null) deque.offer(pop.right);
                if(pop.left != null) deque.offer(pop.left);
                size--;
            }
            res.add(max);
        }
        return res;
    }
}
